package com.roamer.school.web.controller.sys;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.roamer.school.common.validator.group.AddGroup;
import com.roamer.school.common.validator.group.UpdateGroup;
import com.roamer.school.repository.entity.sys.SysUser;
import com.roamer.school.service.model.sys.UserDTO;
import com.roamer.school.service.sys.UserService;
import com.roamer.school.web.model.ResultBean;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 用户管理 控制层
 *
 * @author roamer
 * @version V1.0
 * @date 2018/12/29 14:58
 */
@RestController
@RequestMapping("/api")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "查询单个用户")
    @GetMapping("/users/{id}")
    public ResultBean<UserDTO> findById(@PathVariable("id") Long id) {
        SysUser user = userService.getById(id);
        UserDTO userDTO = UserDTO.convertFor(user);
        return ResultBean.success(userDTO);
    }

    @RequiresPermissions(value = {"user:view"})
    @ApiOperation(value = "查询", notes = "分页查询用户列表", produces = MediaType.APPLICATION_JSON_VALUE)
    @GetMapping(value = "/users/page", params = {"current", "size"}, consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResultBean<IPage<UserDTO>> findPage(
            @ApiParam("分页参数") Page<SysUser> page, @ApiParam("查询条件") @Validated UserDTO userDTO) {
        IPage<SysUser> resultPage = userService.findPage(page, userDTO);
        return ResultBean.success(resultPage.convert(UserDTO::convertFor));
    }


    @RequiresPermissions(value = {"user:add"})
    @ApiOperation(value = "新增", notes = "添加用户", consumes = MediaType.APPLICATION_JSON_VALUE,
                  produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/users", consumes = MediaType.APPLICATION_JSON_VALUE,
                 produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultBean<UserDTO> addUser(@ApiParam("用户信息") @Validated(AddGroup.class) @RequestBody UserDTO userDTO) {
        SysUser user = userService.saveUser(userDTO);
        return ResultBean.success(UserDTO.convertFor(user));
    }

    @RequiresPermissions("user:update")
    @ApiOperation(value = "修改", notes = "修改用户信息", consumes = MediaType.APPLICATION_JSON_VALUE,
                  produces = MediaType.APPLICATION_JSON_VALUE)
    @PutMapping(value = "/users/{id}", consumes = MediaType.APPLICATION_JSON_VALUE,
                params = MediaType.APPLICATION_JSON_VALUE)
    public ResultBean<UserDTO> modifyUser(
            @ApiParam("用户ID") @PathVariable("id") Long userId,
            @ApiParam("修改后的用户信息") @Validated(UpdateGroup.class) @RequestBody UserDTO userDTO) {
        userDTO.setId(userId);
        SysUser user = userService.updateUser(userDTO);
        return ResultBean.success(UserDTO.convertFor(user));
    }

}
